package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.Complement;
import com.burgeon.backend.SHOP.service.AppComplementService;
import com.burgeon.backend.entity.*;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("shop/complement")
public class AppComplementController extends BaseController{
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AppComplementService appComplementService;

    @RequestMapping("retailrecorditem")
    public ResponseResult<ArrayList<RetailRecordItem>> GetRetailRecord(String id, HttpSession session) {
        String token = session.getAttribute("token").toString();

        ArrayList<RetailRecordItem> data = appComplementService.getVipRetailItem(id, token);
        return new ResponseResult<ArrayList<RetailRecordItem>>(SUCCESS, data);
    }

    @RequestMapping("userdata")
    public ResponseResult<User> getUserData(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        User data = appComplementService.getUserData(openid,token);

        return new ResponseResult<User>(SUCCESS,data);
    }

    /**
     * 指定专属店员
     * @param session
     * @return
     */
    @RequestMapping("makemp")
    public ResponseResult<Void> setEmp(String emp,HttpSession session) {

        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        appComplementService.modUserEmp(phone,openid,emp,token);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 获取店员
     * @param session
     * @return
     */
    @RequestMapping("emp")
    public ResponseResult<HashMap> empData(HttpSession session) {

        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        HashMap data = appComplementService.userEmpData(openid,token);

        return new ResponseResult<HashMap>(SUCCESS,data);
    }

    /**积分任务*/
    /**
     * VipAttrib+codeId=code(选中标签名)
     * @param vipAttrib
     * @param cid
     * @param session
     * @return
     */

    @RequestMapping("maketags")
    public ResponseResult<Void> makComplement(VipAttrib vipAttrib, String cid, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();

        appComplementService.makeUserTags(phone,vipAttrib,cid,token);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 标签选项 会员任务 传入标签name
     * @param code
     * @param session
     * @return
     */
    @RequestMapping("tagsitem")
    public ResponseResult<List> GetTagsItem(String code,HttpSession session){

        String token = session.getAttribute("token").toString();
        ArrayList<String> data = appComplementService.getTagsItem(code,token);

        return new ResponseResult<List>(SUCCESS, data);
    }

    /**
     * 显示标签 use=Y 已领取
     * @param session
     * @return
     */
    @RequestMapping("vipshow")
    public ResponseResult<List<Complement>> VipShowData(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String account = accountMapper.GetAccountForToken(token);
        String phone = session.getAttribute("phone").toString();
        List<Complement> data = appComplementService.getComplementSwitch(phone,account);

        return new ResponseResult<List<Complement>>(SUCCESS, data);
    }

    @RequestMapping("getphone")
    public ResponseResult<String> getphone(HttpSession session) {
        String data = session.getAttribute("phone").toString();
        return new ResponseResult<String>(SUCCESS, data);
    }

    /**
     * 修改会员资料
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("vipmod")
    public ResponseResult<Void> modVipData(User user, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();

        appComplementService.modVipData(token, user, openid);
        return new ResponseResult<>(SUCCESS);
    }
//xx
    @RequestMapping("del")
    public ResponseResult<Void> delComplement(String id) {
        appComplementService.delComplement(id);
        return new ResponseResult<>(SUCCESS);
    }
//xx
    @RequestMapping("show")
    public ResponseResult<List> ShowData(String account) {
        //String account = session.getAttribute("account").toString();
        List data = appComplementService.getComplement(account);

        return new ResponseResult<List>(SUCCESS, data);
    }
//xx
    @RequestMapping("create")
    public ResponseResult<Void> addComplementSwitch(Complement complement, String account) {
        //String account = session.getAttribute("account").toString();
        String token = accountMapper.GetTokenForAccount(account);
        complement.setAccount(account);
        complement.setToken(token);
        appComplementService.newComplementSwitch(complement);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 积分兑换
     */
    //兑换购物券
    @RequestMapping("conversion")
    public ResponseResult<Void> Conversion(String tid, HttpSession session) {

        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }

        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        String openid = session.getAttribute("app_openid").toString();
        appComplementService.PointsAreRedeemedForCoupons(tid,phone,openid,token);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * @author: 伯俊软件
     * @description: 套券积分兑换
     * @date: 2021/6/23 15:40
     * @param tids
     * @param session
     * @return com.burgeon.backend.stsutil.ResponseResult<java.lang.Void>
     */
    @RequestMapping("giftConversion")
    public ResponseResult<Void> giftConversion(String cid, String[] tids, HttpSession session) throws Exception {

        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }

        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        String openid = session.getAttribute("app_openid").toString();
        appComplementService.PointsAreRedeemedForGiftCoupons(cid,tids,phone,openid,token);
        return new ResponseResult<>(SUCCESS);
    }


    //单券显示积分兑换
    @RequestMapping("showconversion")
    public ResponseResult<List> ShowConversion(HttpSession session) {
        String token = session.getAttribute("token").toString();
        List data = appComplementService.showRedeemCoupons(token);
        return new ResponseResult<>(SUCCESS, data);
    }


    /**
     * @author: 伯俊软件
     * @description: 套券积分展示
     * @date: 2021/6/23 9:51
     * @param session
     * @return
     */
    @RequestMapping("showGiftConversion")
    public ResponseResult<List> showGiftConversion(HttpSession session) {
        String token = session.getAttribute("token").toString();
        System.err.println("token==="+token);
        List data = appComplementService.showGiftConversion(token);
        return new ResponseResult<>(SUCCESS, data);
    }


    /**积分明细*/
    @RequestMapping("recharge")
    public ResponseResult<ArrayList<RechargeItem>> GetVIPRecharge(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        //phone = "17732141036";//test
        ArrayList<RechargeItem> data = appComplementService.getVipRechargeItem(phone,token);
        return new ResponseResult<ArrayList<RechargeItem>>(SUCCESS,data);
    }

    /**
     * 消费记录
     */
    @RequestMapping("retailrecord")
    public ResponseResult<ArrayList<RetailRecord>> GetRetailRecord(HttpSession session){
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
       // phone = "13081146168";
        ArrayList<RetailRecord> data = appComplementService.getVipRetail(phone,token);
        return new ResponseResult<ArrayList<RetailRecord>>(SUCCESS,data);
    }


}
